package com.mobilesales.android.adapters;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.mobilesales.android.R;
import com.mobilesales.android.activities.tasks.ListTask;
import com.mobilesales.android.util.CalendarDayModel;
import com.mobilesales.android.util.Constants;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CalendarAdapter extends ArrayAdapter<CalendarDayModel> {

	List<CalendarDayModel> days;
	private static final int holidayColor = 0xfff76a51; 
	private static final int invisibleColor = 0xff000000; 
	private static final int labelColor = 0xffadadad; 
	private static final int currentColor = 0xfff3ff4f; 
	private static final int taskColor = 0xff55b3fa; 
	

	public CalendarAdapter(Context context, int textViewResourceId,
			List<CalendarDayModel> objects) {
		super(context, textViewResourceId, objects);
		days = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.calendar_day, null);
		}

		CalendarDayModel day = null;
		if (position >= 0 && position < days.size()) {
			day = days.get(position);
		}

		if (day != null) {
			RelativeLayout container = (RelativeLayout) v
					.findViewById(R.id.dayContainer);
			TextView dayField = (TextView) v.findViewById(R.id.dayTxt);
			dayField.setText(day.getLabel());
			
			if (day.getColor() != 0)
				container.setBackgroundColor(day.getColor());
			
			//if (day.hasTasks()) {
				final Date date = day.getDate();
				final Context ctx = this.getContext();
				container.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {
						Intent intent = new Intent(ctx, ListTask.class);
						intent.putExtra(Constants.SelectedDate, date);
						ctx.startActivity(intent);
					}
				});
			//}
		}
		return v;
	}
	
	public static CalendarAdapter generateAdapter(Context ctx, int year, int month) {
		return generateAdapter(ctx,year,month,null);
	}

	public static CalendarAdapter generateAdapter(Context ctx, int year, int month, List<Integer> taskDates) {
		
		ArrayList<CalendarDayModel> days = new ArrayList<CalendarDayModel>();
		int[] dayNameResIds = new int[] { R.string.Mon, R.string.Tue, R.string.Wed, R.string.Thu,
										  R.string.Fri, R.string.Sat, R.string.Sun	};
		
		for(int i=0 ; i<dayNameResIds.length; i++) {
			String dayName = ctx.getString(dayNameResIds[i]);
			CalendarDayModel day = new CalendarDayModel();
			day.setLabel(dayName);
			day.setColor(CalendarAdapter.labelColor);
			days.add(day);
		}
		
		month = month-1;
		GregorianCalendar calendar = new GregorianCalendar(year,month,1);
		int daysInMonth = calendar.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
		int dayOfWeekFirst = 9-calendar.get(GregorianCalendar.DAY_OF_WEEK);
		
		calendar.set(year, month, daysInMonth);
		int dayOfWeekLast = calendar.get(GregorianCalendar.DAY_OF_WEEK)-1;

		if (dayOfWeekFirst==8) dayOfWeekFirst = 1;
		for(int i=0; i<7-dayOfWeekFirst; i++) {
			CalendarDayModel day = new CalendarDayModel();
			days.add(day);
			day.setColor(CalendarAdapter.invisibleColor);
		}
		Date now = new Date();
		for(int i=1; i<=daysInMonth; i++) {
			calendar.set(year, month, i);
			Date d = calendar.getTime();
			CalendarDayModel day = new CalendarDayModel();
			day.setLabel(String.valueOf(i));
			
			if (taskDates.contains(i)) {
				day.setColor(CalendarAdapter.taskColor);
				day.hasTasks(true);
			}
			else if (now.getYear() == d.getYear() &&
				now.getDate() == d.getDate() &&
				now.getMonth() == d.getMonth()) {
				day.setColor(CalendarAdapter.currentColor);
			}
			else if (calendar.get(GregorianCalendar.DAY_OF_WEEK) == GregorianCalendar.SUNDAY ||
				calendar.get(GregorianCalendar.DAY_OF_WEEK) == GregorianCalendar.SATURDAY) {
				day.setColor(CalendarAdapter.holidayColor);
			}
			day.setDate(calendar.getTime());
			days.add(day);
		}
		for(int i=0; i<7-dayOfWeekLast; i++) {
			CalendarDayModel day = new CalendarDayModel();
			days.add(day);
			day.setColor(CalendarAdapter.invisibleColor);
		}	
		
		return new CalendarAdapter(ctx, R.layout.calendar_day, days);
	}

}
